﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

using System.Web;
using DotNetNuke.Modules.ActiveForums.Data;
using Microsoft.ApplicationBlocks.Data;
using DotNetNuke.Services.Journal;
namespace DotNetNuke.Modules.ActiveForums
{
	public class Social : DataConfig
	{
		public int GetActiveSocialStatus(int PortalId)
		{
#if SKU_LITE
			//Not Available
			return -2;
#else
			if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")))
			{
				DotNetNuke.Entities.Modules.ModuleController mc = new DotNetNuke.Entities.Modules.ModuleController();
				DotNetNuke.Entities.Tabs.TabController tc = new DotNetNuke.Entities.Tabs.TabController();
				DotNetNuke.Entities.Tabs.TabInfo ti = null;
				foreach (DotNetNuke.Entities.Modules.ModuleInfo mi in mc.GetModules(PortalId))
				{
					if (mi.ModuleName.Contains("Active Social") && mi.IsDeleted == false)
					{
						ti = tc.GetTab(mi.TabID, PortalId, false);
						if (ti != null)
						{
							if (ti.IsDeleted == false)
							{
								//Installed and ready
								return 1;
							}
						}
					}
				}
				//Not installed on portal
				return 0;
			}
			else
			{
				//Not Installed
				return -1;
			}
#endif
		}
		public void AddTopicToJournal(int PortalId, int ModuleId, int ForumId, int TopicId, int UserId, string URL, string Subject, string Summary, string Body, int SecurityOption, string ReadRoles, int SocialGroupId)
		{

			JournalItem ji = new JournalItem();
			ji.PortalId = PortalId;
			ji.ProfileId = UserId;
			ji.UserId = UserId;
			ji.Title = Subject;
			ji.ItemData = new ItemData();
			ji.ItemData.Url = URL;
			if (string.IsNullOrEmpty(Summary))
			{
				Summary = Utilities.StripHTMLTag(Body);
				if (Summary.Length > 150)
				{
					Summary = Summary.Substring(0, 150) + "...";
				}
			}
			ji.Summary = Summary;
			ji.Body = Body;
			ji.JournalTypeId = 5;
			ji.ObjectKey = string.Format("{0}:{1}", ForumId.ToString(), TopicId.ToString());
			if (! (JournalController.Instance.GetJournalItemByKey(PortalId, ji.ObjectKey) == null))
			{
				JournalController.Instance.DeleteJournalItemByKey(PortalId, ji.ObjectKey);
			}
			string roles = string.Empty;
			if (! (string.IsNullOrEmpty(ReadRoles)))
			{
				if (ReadRoles.Contains("|"))
				{
					roles = ReadRoles.Substring(0, ReadRoles.IndexOf("|") - 1);
				}
			}

			foreach (string s in roles.Split(';'))
			{
				if ((s == "-1") | (s == "-3"))
				{
					if (! (ji.SecuritySet.Contains("E,")))
					{
						ji.SecuritySet += "E,";
					}
				}
				else
				{
					ji.SecuritySet += "R" + s + ",";
				}

			}
			if (SocialGroupId > 0)
			{

				ji.SocialGroupId = SocialGroupId;

			}
			JournalController.Instance.SaveJournalItem(ji, -1);


			//If GetActiveSocialStatus(PortalId) < 1 Then
			//    Exit Sub
			//End If
			//Try
			//    Dim ai As Object = Nothing
			//    Dim asm As System.Reflection.Assembly
			//    asm = System.Reflection.Assembly.Load("Active.Modules.Social")
			//    Dim ac As Object = Nothing
			//    ac = asm.CreateInstance("Active.Modules.Social.API.Journal")
			//    Dim MainSettings As SettingsInfo = DataCache.MainSettings(ModuleId)
			//    Dim roles As String = String.Empty
			//    If Not String.IsNullOrEmpty(ReadRoles) Then
			//        If ReadRoles.Contains("|") Then
			//            roles = ReadRoles.Substring(0, ReadRoles.IndexOf("|") - 1)
			//        End If
			//    End If
			//    ac.AddProfileItem(New Guid(MainSettings.ActiveSocialTopicsKey), UserId, URL, Subject, Summary, Body, SecurityOption, roles)
			//Catch ex As Exception

			//End Try
		}
		public void AddReplyToJournal(int PortalId, int ModuleId, int ForumId, int TopicId, int ReplyId, int UserId, string URL, string Subject, string Summary, string Body, int SecurityOption, string ReadRoles, int SocialGroupId)
		{

			JournalItem ji = new JournalItem();
			ji.PortalId = PortalId;
			ji.ProfileId = UserId;
			ji.UserId = UserId;
			ji.Title = Subject;
			ji.ItemData = new ItemData();
			ji.ItemData.Url = URL;
			if (string.IsNullOrEmpty(Summary))
			{
				Summary = Utilities.StripHTMLTag(Body);
				if (Summary.Length > 150)
				{
					Summary = Summary.Substring(0, 150) + "...";
				}
			}
			ji.Summary = Summary;
			ji.Body = Body;
			ji.JournalTypeId = 6;
			ji.ObjectKey = string.Format("{0}:{1}:{2}", ForumId.ToString(), TopicId.ToString(), ReplyId.ToString());
			if (! (JournalController.Instance.GetJournalItemByKey(PortalId, ji.ObjectKey) == null))
			{
				JournalController.Instance.DeleteJournalItemByKey(PortalId, ji.ObjectKey);
			}
			string roles = string.Empty;
			if (! (string.IsNullOrEmpty(ReadRoles)))
			{
				if (ReadRoles.Contains("|"))
				{
					roles = ReadRoles.Substring(0, ReadRoles.IndexOf("|") - 1);
				}
			}

			foreach (string s in roles.Split(';'))
			{
				if ((s == "-1") | (s == "-3"))
				{
					if (! (ji.SecuritySet.Contains("E,")))
					{
						ji.SecuritySet += "E,";
					}
				}
				else
				{
					ji.SecuritySet += "R" + s + ",";
				}

			}
			if (SocialGroupId > 0)
			{
				ji.SocialGroupId = SocialGroupId;
			}
			JournalController.Instance.SaveJournalItem(ji, -1);

		}
		public void AddForumItemToJournal(int PortalId, int ModuleId, int UserId, string entryType, string sURL, string sSubject, string sBody)
		{
			try
			{



			}
			catch (Exception ex)
			{
				DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
			}
		}
		internal IDataReader ActiveSocialListGroups(int PortalId)
		{


			if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")))
			{
				string sSQL = "Select g.GroupId, g.GroupName from " + _databaseOwner + _objectQualifier + "activesocial_Groups as g WHERE PortalId = " + PortalId.ToString() + " AND StatusId = 1";
				return SqlHelper.ExecuteReader(_connectionString, CommandType.Text, sSQL);
			}
			else
			{
				return null;
			}

		}
		internal string ActiveSocialGroups(int userId, int PortalId)
		{

			string sGroups = string.Empty;
			try
			{
				//Dim MainSettings As SettingsInfo = DataCache.MainSettings(ModuleId)
				if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")))
				{
					DataSet ds = SqlHelper.ExecuteDataset(_connectionString, _databaseOwner + _objectQualifier + "activesocial_Groups_ListMyGroups", PortalId, 0, 1000, userId, 1, userId);
					if (ds.Tables.Count > 0)
					{
						DataTable dt = ds.Tables[0];
						if (dt.Rows.Count > 0)
						{
							foreach (DataRow dr in dt.Rows)
							{
								sGroups += dr["GroupId"].ToString() + ":" + dr["GroupMemberTypeId"].ToString() + ";";
							}
						}
					}
					else
					{
						return string.Empty;
					}
				}
				else
				{
					return string.Empty;
				}
			}
			catch (Exception ex)
			{
				DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
			}
			return sGroups;

		}
		internal string ActiveSocialGetGroup(int groupId)
		{

			string sGroups = string.Empty;
			try
			{
				if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")))
				{
					sGroups = Convert.ToString(SqlHelper.ExecuteScalar(_connectionString, _databaseOwner + _objectQualifier + "activesocial_Groups_GetName", groupId));
					return sGroups;
				}
			}
			catch (Exception ex)
			{
				DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
			}
			return sGroups;
		}
		internal string ActiveSocialGetGroupType(int groupTypeId, int PortalId)
		{

			string sGroups = string.Empty;
			try
			{
				if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")))
				{
					using (IDataReader dr = SqlHelper.ExecuteReader(_connectionString, _databaseOwner + _objectQualifier + "activesocial_Groups_GetMemberTypes", PortalId))
					{
						while (dr.Read())
						{
							if (Convert.ToInt32(dr["GroupMemberTypeId"].ToString()) == groupTypeId)
							{
								sGroups = dr["MemberTypeName"].ToString();

							}
						}
						dr.Close();
					}
					return sGroups;
				}
			}
			catch (Exception ex)
			{
				DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
				//Return ex.Message
			}
			return sGroups;
		}
		internal int GetMemberTabId(int PortalId)
		{

			try
			{
				if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")))
				{
					string sSQL = "SELECT SettingValue FROM " + _databaseOwner + _objectQualifier + "activesocial_Settings WHERE SettingName='MEMBERSTABID' AND PortalId = " + PortalId;
					return Convert.ToInt32(SqlHelper.ExecuteScalar(_connectionString, CommandType.Text, sSQL));
				}
				return -1;
			}
			catch (Exception ex)
			{
				DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
			}
			return -1;

		}
		internal int GetMasterForumGroup(int PortalId, int CurrentTabId)
		{
			int ForumGroupId = -1;
			int GroupViewTabId = -1;

            try
			{
				if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/bin/active.modules.social.dll")))
				{
					string sSQL = "SELECT SettingValue FROM " + _databaseOwner + _objectQualifier + "activesocial_Settings WHERE SettingName='GLOBALGROUPFORUMTEMPLATEGROUP' AND PortalId = " + PortalId;
					ForumGroupId = Convert.ToInt32(SqlHelper.ExecuteScalar(_connectionString, CommandType.Text, sSQL));
					sSQL = "SELECT SettingValue FROM " + _databaseOwner + _objectQualifier + "activesocial_Settings WHERE SettingName='GROUPSVIEWTABID' AND PortalId = " + PortalId;
					GroupViewTabId = Convert.ToInt32(SqlHelper.ExecuteScalar(_connectionString, CommandType.Text, sSQL));
					if (GroupViewTabId == CurrentTabId)
					{
						ForumGroupId = -1;
					}
				}
			}
			catch (Exception ex)
			{
				DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
			}
			return ForumGroupId;
		}
	}
}
